#!/usr/bin/python
# -*- coding: utf8 -*-

from sets import Set

cadena = raw_input('Ingrese la cadena a analizar: ') + '$'
posicion = 0

class ParseError(Exception):
    def __init__(self, esperado):
        self.esperado = esperado
    def __str__(self):
        return "----------------------\n" + \
               "Error de sintaxis\n" + \
               "----------------------\n" + \
               "Se esperaba recibir alguno de: %s\n" % list(self.esperado) + \
               "Se recibió: '%s'\n" % tc() + \
               "Ubicación: %s\n" % cadena + \
               (" " * (11 + posicion)) + '^'


def tc():
    global posicion
    global cadena
    return cadena[posicion]

def match(c):
    global posicion
    global cadena
    if cadena[posicion] == c:
        posicion += 1
    else:
        raise ParseError(c)


def noterm_C():
    noterm_U()
    noterm_W()

def noterm_E():
    if tc() in Set(['+']):
        match('+')
        noterm_E()
    elif tc() in Set(['?', '*']):
        if tc() in Set(['*']):
            match('*')
            noterm_E()
        elif tc() in Set(['?']):
            if tc() in Set(['?']):
                match('?')
                noterm_E()

def noterm_D():
    noterm_C()
    noterm_E()

def noterm_G():
    noterm_P()
    noterm_J()

def noterm_F():
    if tc() in Set("ABCDEFGHIJKLMNOPQRSTUVWXYZ"):
        match(tc())
    elif tc() in Set('('+ "abcdefghijklmnopqrstuvwxyz"):
        if tc() in Set("abcdefghijklmnopqrstuvwxyz"):
            match(tc())
        elif tc() in Set('('):
            
            if tc() in Set('('):
                match('(')
                noterm_D()
                match(')')

def noterm_J():
    if tc() in Set("ABCDEFGHIJKLMNOPQRSTUVWXYZ"):
        noterm_G()

def noterm_P():
    if tc() in "ABCDEFGHIJKLMNOPQRSTUVWXYZ":
        match(tc())
    else:
        parseError(Set("ABCDEFGHIJKLMNOPQRSTUVWXYZ"))
    match(':')
    noterm_D()
    match(';')

def noterm_U():
    noterm_F()
    noterm_V()

def noterm_W():
    if tc() in Set('(' + "abcdefghijklmnopqrstuvwxyz" + "ABCDEFGHIJKLMNOPQRSTUVWXYZ"):
        noterm_U()
        noterm_W()

def noterm_V():
    if tc() in Set(['|']):
        match('|')
        noterm_F()
        noterm_V()

try:
    noterm_G()
    match('$')
    print 'OK!'
except ParseError, e:
    print e

